package com.xdwanj.service.impl;

import com.xdwanj.dao.AdminUserDao;
import com.xdwanj.dao.EmpDao;
import com.xdwanj.dao.impl.AdminUserDaoImpl;
import com.xdwanj.dao.impl.EmpDaoImpl;
import com.xdwanj.pojo.Emp;
import com.xdwanj.service.EmpService;
import com.xdwanj.util.PageBean;

import java.util.List;

public class EmpServiceImpl implements EmpService {
    EmpDao empDao = new EmpDaoImpl();
    AdminUserDao adminUserDao = new AdminUserDaoImpl();

    @Override
    public PageBean<Emp> findEmpList(String queryName, int roid, int currentPageNo, int PageSize) {
        PageBean<Emp> empList = null;
        try {
            empList = empDao.findEmpList(queryName, roid, currentPageNo, PageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return empList;
    }

    @Override
    public boolean updateEmp(Emp emp, List<Integer> ridList) {
        int updateRow = 0;
        boolean flag = false;

        try {
            updateRow = empDao.updateEmp(emp);

            if (ridList != null && ridList.size() != 0) {
                empDao.deleteEmpAndRole(emp.getId());
                empDao.insertEmpAndRole(emp.getId(), ridList);
            } else if (ridList != null && ridList.size() == 0) {
                empDao.deleteEmpAndRole(emp.getId());
            }

            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (updateRow == 0) {
            flag = false;
        }

        return flag;
    }

    @Override
    public boolean deleteEmpAndAdminUserByEmpId(int eid) {
        int delEmpRow = 0;
        boolean flag = false;

        try {
            adminUserDao.deleteByEmpId(eid);
            delEmpRow = empDao.deleteEmp(eid);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (delEmpRow == 0) {
            flag = false;
        }

        return flag;
    }

    @Override
    public Emp addEmp(Emp emp, List<Integer> ridList) {
        Emp tableEmp = null;

        try {
            tableEmp = empDao.addEmp(emp, ridList);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return tableEmp;
    }
}
